package localizer.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;

public class RingActivity extends Activity{
	public static long TIME_TO_WAIT = 1000 * 8;
	MediaPlayer mp ;
	Config config;
	Thread threadBright;
	boolean continua;
	Vibrator vibrator;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ring_silent_mode);
		
		continua=true;
		vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		
		Window window = getWindow();
		window.addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
		window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
		window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		window.addFlags(LayoutParams.FLAG_FULLSCREEN);
		
		config = Config.getInstance(getApplicationContext());
		TIME_TO_WAIT = Long.parseLong(config.getTimeRing(getApplicationContext())) * 1000;
		Button btn = (Button) findViewById(R.id.ok_silent_mode);
		if(getIntent().getExtras().getString("tipe").equals("ring")){
			AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,  audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) ,AudioManager.FLAG_VIBRATE);
			mp = MediaPlayer.create(getApplicationContext(), R.raw.telephonering);
			mp.setVolume(1, 1);
			mp.setLooping(true);
			mp.start();
		}
		
		final Handler handler = new Handler(){
        	@Override
        	public void handleMessage(Message msg) {
        		fim();
        	}

        };
		try{
		Thread th = new Thread(){
			public void run(){
				try {
					sleep(TIME_TO_WAIT);
				} catch (InterruptedException e) {
				}
				Message m = handler.obtainMessage();
				handler.sendMessage(m);
			}
		};
		th.start();
		} catch ( Exception e){
			Log.d("erro na criacao da thread", e.getMessage());
		}
		
		
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				continua=false;
				fim();
			}
		});
		
		try{
			threadBright = new Thread(){
				public void run(){
					try{
						long inicio = System.currentTimeMillis();
						float brilho = 0.02f;
						while(continua && System.currentTimeMillis() - inicio < TIME_TO_WAIT){
							vibrator.vibrate(200);
							Message m = handlerChangeBright.obtainMessage();
							m.obj=new Float(brilho);
							handlerChangeBright.sendMessage(m);
							if(brilho < 1){
								brilho=1;
								sleep(200);
							}else{ 
						 		brilho =0.02f;
								sleep(2000);
							}
							//Log.e("Brilho"," "+brilho);
							
						}
						Message m = handler.obtainMessage();
						handler.sendMessage(m);
						} catch(Exception e){
							Log.e("erro na criacao da thread", e.getMessage());
					}
				}
			};
			threadBright.start();
			} catch ( Exception e){
				Log.e("erro na criacao da thread", e.getMessage());
			}
	}
	
	public void fim(){
		Log.d("ring", "encerrou");
		try {
			if(getIntent().getExtras().getString("tipe").equals("ring")){
				mp.stop();			
				mp.release();
				mp = null;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			continua = false;
			finish();
		}
		
	}
	final Handler handlerChangeBright = new Handler(){
    	@Override
    	public void handleMessage(Message msg) {
    		float  brilho = (Float) msg.obj;
    		Window window = getWindow(); 
			 LayoutParams layoutParams = window.getAttributes(); 
			 layoutParams.screenBrightness = brilho; 
			 window.setAttributes(layoutParams);
    	}

    };
    
    protected void onPause() {
    	fim();
    	super.onPause();
    };
}
